package org.dromara.learning_lhz.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.learning_lhz.entity.AsStudentAdminEntity;
import org.dromara.learning_lhz.entity.AsStudyPlanEntity;
import org.dromara.learning_lhz.entity.dto.StudyPlanDto;
import org.dromara.learning_lhz.entity.vo.StudyPlanVo;
import org.dromara.learning_lhz.mapper.StudyPlanMapper;
import org.dromara.learning_lhz.service.StudyPlanService;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class StudyPlanServiceImpl extends MPJBaseServiceImpl<StudyPlanMapper, AsStudyPlanEntity> implements StudyPlanService {
    @Resource
    private StudyPlanMapper studyPlanMapper; // 注入你的 Mapper

    /**
     * 查询
     *
     * @param studyPlanDto
     * @return
     */
    @Override
    public Page<StudyPlanVo> selStudyPlan(StudyPlanDto studyPlanDto, PageQuery pageQuery) {
        MPJLambdaWrapper<AsStudyPlanEntity> wrapper = new MPJLambdaWrapper<>();

        // LEFT JOIN AsStudentAdminEntity 表，通过 student_id 和 id 关联
        wrapper.eq(Objects.nonNull(studyPlanDto.getId()), AsStudyPlanEntity::getId, studyPlanDto.getId())
            .select(AsStudyPlanEntity::getId, AsStudyPlanEntity::getStudentId, AsStudyPlanEntity::getSubject, AsStudyPlanEntity::getStudyPlan)  // 选择学习计划表中的字段
            .select(AsStudentAdminEntity::getStudentName) // 选择学生表中的字段
            .leftJoin(AsStudentAdminEntity.class, AsStudentAdminEntity::getId, AsStudyPlanEntity::getStudentId);

        // 根据学生姓名模糊查询
        if (studyPlanDto.getStudentName() != null) {
            wrapper.like(AsStudentAdminEntity::getStudentName, studyPlanDto.getStudentName());
        }

        // 执行分页查询
        Page<StudyPlanVo> studyPlanVoPage = studyPlanMapper.selectJoinPage(pageQuery.build(), StudyPlanVo.class, wrapper);



        // 处理查询结果，将相同 subject 和 studyPlan 的 studentName 合并为 studentNameList
        Map<String, StudyPlanVo> resultMap = new LinkedHashMap<>();

        for (StudyPlanVo record : studyPlanVoPage.getRecords()) {
            // 创建唯一键用于判断 subject 和 studyPlan 是否相同
            String key = record.getSubject() + "_" + record.getStudyPlan();

            // 如果相同 key 的记录已存在，则合并 studentName
            if (resultMap.containsKey(key)) {
                StudyPlanVo existingRecord = resultMap.get(key);
                if (existingRecord.getStudentNameList() == null) {
                    existingRecord.setStudentNameList(new ArrayList<>());
                }
                existingRecord.getStudentNameList().add(record.getStudentName());
            } else {
                // 创建新的记录，并将 studentName 添加到 studentNameList
                record.setStudentNameList(new ArrayList<>());
                record.getStudentNameList().add(record.getStudentName());
                resultMap.put(key, record);
            }
        }

        // 将处理后的结果重新设置为分页对象中的 records
        studyPlanVoPage.setRecords(new ArrayList<>(resultMap.values()));

        return studyPlanVoPage;
    }


    /**
     * 添加 学习计划
     *
     * @param studyPlanDto
     * @return
     */
    @Override
    public void addStudyPlan(StudyPlanDto studyPlanDto) {
        // 遍历 studentId 列表并批量插入记录
        List<AsStudyPlanEntity> planEntities = new ArrayList<>();

        for (Long studentId : studyPlanDto.getStudentId()) {
            AsStudyPlanEntity plan = new AsStudyPlanEntity();
            plan.setSubject(studyPlanDto.getSubject());
            plan.setStudyPlan(studyPlanDto.getStudyPlan());
            plan.setStudentId(studentId);
            // 添加到批量插入列表中
            planEntities.add(plan);
        }

        // 使用 MyBatis-Plus 的批量插入功能
        if (!planEntities.isEmpty()) {
            this.saveBatch(planEntities); // 请确保你的 Mapper 支持批量插入
        }
    }
}
